#include "XMLFormatter.h"
#include <string>
#include<iostream>
#include<vector>
using namespace std;
//asasdadasdsa
void split99(string text,string delmiter,vector<string>* result)
{
	string temp="";
	int delmiterlen=delmiter.size();
	for(int i=0;i<text.size();i++)
	{

		if(text.substr(i,delmiterlen).compare(delmiter)==0)
		{
			if(temp.compare("")!=0)
				result->push_back(temp);
			temp="";
			i+=delmiterlen-1;
		}
		else
		{
			temp+=text[i];
		}
	}
	if(temp.compare("")!=0)
	{
		result->push_back(temp);
	}
}
XMLFormatter::XMLFormatter()
{
}
string XMLFormatter::Formatter(int choice, string word, vector<string> *s)
{
	string temp;
	string parameters="";
	if(s!=NULL)
	{
		for(int i=0; i<s->size(); i++)
		{
			parameters+=" "+s->at(i);
		}
	}

	if(choice == bibliomisc)
		temp = "<bibliomisc"+parameters+">"+word+"</bibliomisc>\n";

	if(choice == score)
		temp = "<score"+parameters+">"+word+"</score>\n";

	if(choice == sentence)
		temp = "<sentence"+parameters+">"+word+"</sentence>";

	if(choice == abstract_)
		temp = "<abstract"+parameters+">\n"+word+"</abstract>\n";

	if(choice == article)
		temp = "<article"+parameters+">"+word+"</article>\n";

	if(choice == title)
		temp = "<title"+parameters+">"+word+"</title>\n";

	if(choice == cons)
		temp = "<span class=\"dropt\"><cons"+parameters+">"+word+"</cons><span><b>"+parameters+"</b><a href=\"#\">&nbsp; more information..</a></span></span>";

	if(choice == date)
		temp = "<date"+parameters+">"+word+"</date>\n";

	if(choice == articleinfo)
		temp = "<articleinfo"+parameters+">\n"+word+"</articleinfo>\n";

	if(choice == year)
		temp = "<year"+parameters+">"+word+"</year>\n";

	if(choice == affilation)
		temp = "<affilation"+parameters+">"+word+"</affilation>\n";

	if(choice == authors)
		temp = "<authors"+parameters+">"+word+"</authors>\n";

	if(choice == Relation_type)
		temp = "<Relation_type"+parameters+">"+word+"</Relation_type>\n";

	if(choice == Relation_type)
		temp = "<Relation_type"+parameters+">"+word+"</Relation_type>\n";

	if(choice == clue)
		temp = "<clue"+parameters+">"+word+"</clue>\n";

	if(choice == arguments)
		temp = "<arguments"+parameters+">"+word+"</arguments>\n";




	return temp;

}
void cleanvector2(vector<string> * input)
{
	for(int i=0;i<input->size();i++)
	{
		input->at(i).erase(0,input->at(i).size());
	}
	input->clear();
}
int XMLFormatter::getStrings(int choice,string XML,vector<string>* output_strings,vector<string>* output_parameters)
{
	string tag;
	if(choice == bibliomisc)
		tag = "bibliomisc";

	else if(choice == score)
		tag="score";

	else if(choice == sentence)
		tag="sentence";

	else if(choice == abstract_)
		tag = "abstract";

	else if(choice == article)
		tag = "article";

	else if(choice == title)
		tag = "title";

	else if(choice == cons)
		tag = "cons";

	else if(choice == date)
		tag = "date";
	else if(choice == articleinfo)
		tag = "articleinfo";
	else if(choice == year)
		tag = "year";
	else if(choice == Pubmed_date)
		tag = "PubDate";
	else if(choice == Pubmed_year)
		tag = "Year";
	else if(choice == Pubmed_title)
		tag = "ArticleTitle";
	else if(choice == Pubmed_affilation)
		tag = "Affiliation";
	else if(choice == Pubmed_id)
		tag = "PMID";
	else if(choice == Pubmed_abstract)
		tag = "AbstractText";
	else if(choice == Pubmed_article)
		tag = "PubmedArticle";
	else if(choice == Pubmed_article_set)
		tag = "PubmedArticleSet";
	else
	{
		cout<<"Unkown tag"<<endl;
		return -2;
	}

	int beginIndex=1;
	if(XML.substr(1,tag.size()).compare(tag)==0)
	{
		beginIndex=0;
	}
	vector<string> SplittedXML;
	SplittedXML.clear();
	split99(XML,"<"+tag,&SplittedXML);
	//	cout<<"/////////////////////////////////////////////"<<endl;
	for(int i=beginIndex;i<SplittedXML.size();i++)
	{
		//	cout<<SplittedXML[i]<<endl;
		size_t pos =SplittedXML[i].find("</"+tag);
		if(pos!=string::npos)
		{
			size_t size=SplittedXML[i].size()-pos;
			string refined_string=SplittedXML[i].substr(0,pos);
			//			cout<<endl<<endl<<refined_string<<endl;

			pos=refined_string.find_first_of('>',0);
			if(pos!=string::npos)
			{
				output_parameters->push_back(refined_string.substr(0,pos));
				output_strings->push_back(refined_string.substr(pos+1,refined_string.size()-pos-1));
			}
			else
			{

				cout<<"error in fromatting"<<endl;
				cleanvector2(&SplittedXML);
				return -3;
			}
		}

		else
		{
			cout<<"end tag not found "<<tag<<endl;
			cleanvector2(&SplittedXML);
			return -4;
		}
	}
	cleanvector2(&SplittedXML);
	return 0;

}
XMLFormatter::~XMLFormatter()
{
}
